home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / clantutr.zip / LESSON12 < prev    next >
Text File  |  1986-02-13  |  6KB  |  161 lines

  1. .NT
  2.  A NOTE ABOUT THE LESSONS in C 
  3. .b4-24R5C4
  4. These were written while the author was ~Ilearning~N  the language and since
  5. .R6C4
  6. they  are  ~Ifree~N ( to  copy  and/or  distribute ) there  is  a money-back
  7. .R7C4
  8. guarantee on the accuracy of each and every statement in the lessons (!)
  9. .R9C4
  10. The  ~Idisplay~N  program was written ( in C ) in order to provide a vehicle
  11. .R10C4
  12. for displaying the lessons.
  13. .R12C5
  14. .B
  15. P.J.Ponzo
  16. .B
  17. Dept. of Applied Math
  18. .B
  19. Univ. of Waterloo
  20. .B
  21. Ontario N2L 3G1
  22. .K16,32
  23. PonzoTUTOR
  24. .WNT
  25.     FIELDS    
  26. .R5C1
  27.     Suppose we wish to manipulate the results of a questionnaire containing
  28.     20 questions with answers either YES or NO. If we store the answers in an
  29.     ~n~Iint~Neger variable ~b~Ix~N, with x=1 corresponding to a YES answer and
  30.     x=0 to a NO, then (since integers occupy 2 bytes of memory, usually) this
  31.     would require 40 bytes per questionnaire and, if 1000 people answered the
  32.     questionnaire the results would occupy 40,000 bytes ... too much!!
  33.  
  34.     ~ISO~N, we store the answers in a 1-byte ~b~Ichar~Nacter variable ~b~Ix~N with
  35.     either  x='Y' or x='N. That would take 20,000 bytes of memory ... too much!
  36.  
  37.     ~ISO~N (since 1=YES and 0=NO only needs ~I1 bit~N of memory) we store the
  38.     results in 20,000 ~Ibits~N ... let's see ... at 8 bits per byte that would
  39.     take ... 20000/8=~I2500~N bytes ... just right.
  40.  
  41.     Is there a DATA TYPE called ~b~Ibit~N (so we could declare ~b~Ibit x;~N) ?
  42.     Alas, there is NOT ... but we ~Ican~N create a ~Istructure~N with members which
  43.     occupy ~Ibits~N of memory!
  44. .WN
  45. ~b~I    struct byte  {    /* define a structure called byte   */ ~N
  46. ~b~I        unsigned member1 : 1;  /*  member occupies  1 bit */ ~N
  47. ~b~I        unsigned member2 : 4;  /*  member occupies 4 bits */ ~N
  48. ~b~I        unsigned member3 : 3;  /*  member occupies 3 bits */ ~N
  49. ~b~I    } x;              /* declare x to be such a structure */ ~N
  50.  
  51.     Now ~b~Ix~N is a structure of type ~b~Ibyte~N which has 3 members (in this example).
  52.     Each member is an ~b~Iunsigned~N integer.
  53.     These members are made to occupy a number of ~Ibits~N.
  54.     The colon ~b~I : ~N followed by an integer (like 1,4 and 3) arranges this for
  55.     us.
  56.  
  57.     Now we can (as usual) refer to ~b~Ix.member1~N, ~b~Ix.member2~N  and ~b~Ix.member3~N
  58.     and (for example) say: ~b~Ix.member1=0;~N or ~b~Ix.member2=13;~N, etc.
  59.  
  60.     Since ~b~Ix.member1~N is 1 bit wide,  it will hold numbers 0 and 1 only.
  61.     Since ~b~Ix.member2~N is 4 bits wide, it will hold numbers 0,1,2,..., 15.
  62.     Since ~b~Ix.member3~N is 3 bits wide, it will hold numbers 0,1,2,..,7.
  63.  
  64. .w
  65.     ... and the whole structure, with all 3 members, only fills one byte of
  66.     memory (since 1+4+3 bits =1 byte).
  67.  
  68.     ~IThe members, occupying a number of adjacent bits in memory, are called~N
  69.                            ~V   FIELDS   ~N
  70. .WN
  71.     If we define another struct called bytes, and declare *x ...
  72. ~b~I    struct bytes {    /* define a structure called bytes  */ ~N
  73. ~b~I        unsigned member1 : 1;  /* member occupies  1 bit  */ ~N
  74. ~b~I        unsigned member2 : 4;  /* member occupies 4 bits  */ ~N
  75. ~b~I        unsigned member3 : 4;  /* member occupies 3 bits  */ ~N
  76. ~b~I    } *x;             /* declare x to be a pointer        */ ~N
  77.     so that ~b~Ix~N is now a ~r~Ipointer~N to a structure, then we would access
  78.     a member with:  ~b~Ix->member1~N  (remember?)
  79.  
  80.     If you're really squeezed for memory space, then cram as much into a
  81.     2-byte integer as possible by using ~Ibits~N.
  82.  
  83.     Note that, in the above structure, we now have 1+4+4 bits which is more
  84.     than a byte will hold, so the compiler will put ~b~Imember3~N into a
  85.     second byte (and "waste" the remaining 3 bits of the first byte and
  86.     probably the remaining 4 bits of the second byte too!)
  87.  
  88. .K17,32
  89. 8bits/byte
  90. .WNT
  91.     UNIONS   
  92. .R5C1
  93.     If you're still in need of memory space you can arrange to have several
  94.     variables occupy the ~Isame~N memory space ... but not at the same time
  95.     (of course).
  96.  
  97.     This union of variables is called a ... ~V  UNION  ~N (what else?)
  98. ~b~I    union sam  {     /* define a union called sam    */ ~N
  99. ~b~I        int x;       /* the first member is an int   */ ~N
  100. ~b~I        float y;     /* the second member is a float */ ~N
  101. ~b~I        char z;      /* the third member is a char   */ ~N
  102. ~b~I    } jeckyl, *hyde; /* declare some unions          */ ~N
  103.  
  104.     Since these 3 variables ~b~Ix~N, ~b~Iy~N and ~b~Iz~N occupy different amounts
  105.     of memory the variable ~b~Ijeckyl~N will occupy sufficient memory to hold
  106.     the largest of ~b~Ix~N, ~b~Iy~N and ~b~Iz~N (in this case, it's ~b~Iy~N).
  107.  
  108.     You may point to a union ( ~b~Ihyde~N is a ~r~Ipointer~N)
  109.     and/or access one of its members ( ~b~Ijeckyl.x=123~N or ~b~Ihyde->z='A'~N ).
  110. .WN
  111. ~b~I    union sam  {     /* define a union called sam    */ ~N
  112. ~b~I        int x;       /* the first member is an int   */ ~N
  113. ~b~I        float y;     /* the second member is a float */ ~N
  114. ~b~I        char z;      /* the third member is a char   */ ~N
  115. ~b~I    } jeckyl, *hyde; /* declare some unions          */ ~N
  116.  
  117.     If you say: ~b~Ijeckyl.x=123~N ( an integer ) then the space set aside for
  118.     ~b~Ijeckyl~N will be occupied (in part) by the integer ~b~I123~N.
  119.  
  120.     If you then say: ~b~Iprintf("%f",jeckyl.y);~N the printf function will go
  121.     to the memory location where jeckyl lives, interpret the 2-byte ~b~Iint~N 
  122.     ~I123~N as a 7-byte float ('cause we said ~b~I%f~N) and print garbage!
  123. .K19,60
  124.   MORAL?
  125. .W
  126. ~V                                                                          ~N
  127. ~V    jeckyl and *hyde will contain an int, float or char depending upon    ~N
  128. ~V      whether the last thing you put there was an int, float or char!     ~N
  129. ~V                                                                          ~N
  130. .WN
  131.  
  132.     So, keep track with some variable called WhoThere.
  133.  
  134. ~b~I#define  CHAR     1 ~N
  135. ~b~I#define  INTEGER  2 ~N
  136. ~b~I#define  FLOAT    3 ~N
  137. ~b~I .................. ~N
  138. ~b~Iint WhoThere;       ~N
  139. ~b~I .................. ~N
  140.  
  141.     Each time you say:
  142.  
  143.     ~b~Ihyde->y=12.34;~N then also say: ~b~IWhoThere=FLOAT;~N
  144.  
  145.     so you can check if ~b~IWhoThere==CHAR~N or ~b~IWhoThere==INTEGER~N etc.
  146.  
  147.     to see who's currently a member of this union!
  148. .K3,60
  149.  easy eh?
  150. .WN
  151.  
  152.  
  153. .T
  154.    That's all folks!   
  155. .K16,32
  156. au revoir!
  157.  
  158.  
  159. .q
  160.  
  161.